﻿//    Copyright (C) 2013 Jay Herman

//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.

//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.

//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace APKExtractor
{
    public partial class FolderPicker : Form
    {
        private string folderPicked = "";

        public string FolderPicked
        {
            get {return folderPicked; }
        }

        public FolderPicker()
        {
            InitializeComponent();
            dropDownOpenRails.Items.AddRange(ORTS.Menu.Folder.GetFolders().ToArray());
            if (dropDownOpenRails.Items.Count == 0)
            {
                radioButtonDirectory.Checked = true;
                radioButtonOpenRails.Enabled = false;
                dropDownOpenRails.Enabled = false;
            }
            else
                dropDownOpenRails.SelectedIndex = 0;
        }

        private void radioButtonOpenRails_CheckedChanged(object sender, EventArgs e)
        {
            dropDownOpenRails.Enabled = radioButtonOpenRails.Checked;
        }

        private void radioButtonDirectory_CheckedChanged(object sender, EventArgs e)
        {
            textBoxDirectory.Enabled = radioButtonDirectory.Checked;
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            if (radioButtonOpenRails.Checked)
            {
                if (dropDownOpenRails.SelectedItem == null)
                {
                    MessageBox.Show("No profiles selected.", Application.ProductName);
                    return;
                }
                ORTS.Menu.Folder openRailsFolder = (ORTS.Menu.Folder)dropDownOpenRails.SelectedItem;

                folderPicked = openRailsFolder.Path;
            }
            else
            {
                if (textBoxDirectory.Text.Length == 0)
                {
                    MessageBox.Show("No directory name given.", Application.ProductName);
                    return;
                }
                folderPicked = textBoxDirectory.Text;
            }
            DialogResult = DialogResult.OK;
            this.Close(); 
        }

        private void buttonBrowse_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
                textBoxDirectory.Text = folderBrowserDialog.SelectedPath;
        }
    }
}
